package com.base.recyclerview

import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView

class DragCallback : ItemTouchHelper.Callback() {
    override fun getMovementFlags(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder
    ): Int {
        val dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN or
                ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT or
                ItemTouchHelper.START or ItemTouchHelper.END

        return makeMovementFlags(dragFlags, 0)
    }

    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean {
        //val adapter = recyclerView.adapter as DesktopAdapter
        //adapter.moveItem(viewHolder.adapterPosition, target.adapterPosition)
        return true
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {}
}

/**
 *
 * class DesktopActivity : AppCompatActivity() {
 *     private lateinit var binding: ActivityDesktopBinding
 *     private lateinit var adapter: AppAdapter
 *     private val vibrator by lazy { getSystemService(VIBRATOR_SERVICE) as Vibrator }
 *
 *     override fun onCreate(savedInstanceState: Bundle?) {
 *         super.onCreate(savedInstanceState)
 *         binding = ActivityDesktopBinding.inflate(layoutInflater)
 *         setContentView(binding.root)
 *
 *         adapter = AppAdapter(mutableListOf(
 *             AppItem(R.drawable.ic_chat, "微信"),
 *             AppItem(R.drawable.ic_music, "音乐"),
 *             // 其他应用项...
 *         ))
 *
 *         binding.recyclerView.apply {
 *             layoutManager = GridLayoutManager(this@DesktopActivity, 4)
 *             adapter = this@DesktopActivity.adapter
 *             ItemTouchHelper(DragTouchHelper()).attachToRecyclerView(this)
 *         }
 *     }
 *
 *     inner class DragTouchHelper : ItemTouchHelper.Callback() {
 *         override fun getMovementFlags(
 *             recyclerView: RecyclerView,
 *             viewHolder: RecyclerView.ViewHolder
 *         ) = makeMovementFlags(
 *             ItemTouchHelper.UP or ItemTouchHelper.DOWN or
 *             ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT,
 *             ItemTouchHelper.START or ItemTouchHelper.END
 *         )
 *
 *         override fun onMove(
 *             recyclerView: RecyclerView,
 *             viewHolder: RecyclerView.ViewHolder,
 *             target: RecyclerView.ViewHolder
 *         ): Boolean {
 *             adapter.moveItem(viewHolder.adapterPosition, target.adapterPosition)
 *             return true
 *         }
 *
 *         override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
 *             vibrator.vibrate(VibrationEffect.createOneShot(50, 255))
 *             adapter.removeItem(viewHolder.adapterPosition)
 *         }
 *
 *         override fun isLongPressDragEnabled() = true
 *     }
 * }
 * */